﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StartStop.ServiceResources;
using System.Data.SqlClient; 
using System.Data; 

namespace StartStop.Helpers
{
    public class Audio
    {
        public const string LiveConnectionString = "data source=v2.startstop.me;initial catalog=startstop;persist security info=True;user id=sa;password=PASSWORD;";

        /// <summary>
        /// This should look up the audio track, if it doesn't exist then it should create it
        /// Ideally this should try not to add duplicate tracks
        /// </summary>
        /// <param name="oAudio"></param>
        
        /// <returns></returns>
        public static ServiceResources.MessageResponse AddAudioTrack(ServiceResources.AudioContent oAudio)
        {
            MessageResponse oResponse = new MessageResponse();
            oResponse.Success = false;
            if (oAudio.SongTitle != "")
            {
                SqlConnection oConn = new SqlConnection(LiveConnectionString);
                oConn.Open(); 
                // We should really add the artist and almbun here, but for now we'll go inelegant
                SqlCommand cmd = new SqlCommand(" Select AudioContentID from AudioContent where SongTitle = @SongTitle", oConn);
                cmd.Parameters.AddWithValue("@SongTitle", oAudio.SongTitle);
                if (cmd.ExecuteScalar() != null)
                {
                    // This is already exists
                    oResponse.Success = true;
                    // Return the ID
                    oResponse.ReturnID = Int64.Parse(cmd.ExecuteScalar().ToString()); 
                }
                else
                {
                    // This needs to be added
                    cmd = new SqlCommand(@"insert into AudioContent (SongTitle,AlbumnTitle,Genre,Rating,Notes,Year,CreatedOn,UserID) 
                                                             Values (@SongTitle,@AlbumnTitle,@Genre,@rating,@Notes,@Year,GetDate(),@UserID); select @@IDENTITY;
                                          ",oConn);

                    cmd.Parameters.AddWithValue("@SongTitle", oAudio.SongTitle);
                    cmd.Parameters.AddWithValue("@AlbumnTitle", oAudio.AlbumnTitle);
                    cmd.Parameters.AddWithValue("@Genre", oAudio.Genre);
                    if (oAudio.Rating != null)
                    {
                        cmd.Parameters.AddWithValue("@Rating", oAudio.Rating); 
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@Rating", DBNull.Value); 
                    }
                    if (oAudio.Notes != null)
                    {
                        cmd.Parameters.AddWithValue("@Notes", oAudio.Notes);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@Notes", DBNull.Value); 
                    }
                    if (oAudio.Year != null)
                    {
                        cmd.Parameters.AddWithValue("@Year", oAudio.Year);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@Year", DBNull.Value);
                    }
                    if (oAudio.ReportingUserID.ToString() != "")
                    {
                        cmd.Parameters.AddWithValue("@UserID", oAudio.ReportingUserID);
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@UserID", DBNull.Value);
                    }

                    string _addresult = cmd.ExecuteScalar().ToString();
                    // it worked.
                    if (_addresult != "")
                    {
                        oResponse.Success = true;
                        oResponse.ReturnID = Int64.Parse(_addresult); 
                    }
                    oConn.Close();                     
                }
               
            }
            else
            {
                oResponse.Message = "Song title cannot be empty"; 
            }

            return oResponse; 
        }

        public static ServiceResources.AudioContent LoadAudioContentFromID(Int64 AudioContentID)
        {
            ServiceResources.AudioContent oContent = new AudioContent();

            return oContent; 
        }

    }
}
